﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.Configuration
{
    public partial class ManagerUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Session["Code"] != null)
                {
                    GetUserRole();
                    HideFunctionality();
                }
                else
                {
                    Response.Redirect("~/Index.aspx");
                }
            }
            CheakFunctionality();
        }

        private void CheakFunctionality()
        {
            string fun = Request.QueryString["fun"];
            switch (fun)
            {
                case "create": createUser.Visible = true; break;
                case "changePass": changePass.Visible = true; break;
                case "reset": reset.Visible = true; break;
                case "delete": delete.Visible = true; break;
                case "edit": edit.Visible = true; break;
                case "search": search.Visible = true; break;
            }
        }

        private void HideFunctionality()
        {
            createUser.Visible = false;
            changePass.Visible = false;
            reset.Visible = false;
            delete.Visible = false;
            edit.Visible = false;
            search.Visible = false;
        }

        private void GetUserRole()
        {
            try
            {
                List<Role> roles = new List<Role>();
                RoleManager manager = new RoleManager();
                roles = manager.GetRoles();
                //Loading Select for Create User
                selectRole.DataTextField = "RoleNme";
                selectRole.DataValueField = "Id";
                selectRole.DataSource = roles;
                selectRole.DataBind();

                //Loading ddlSelectForEdit
                ddlSelectForEdit.DataTextField = "RoleNme";
                ddlSelectForEdit.DataValueField = "Id";
                ddlSelectForEdit.DataSource = roles;
                ddlSelectForEdit.DataBind();

                selectRole.Items.Insert(0, "--Select a Role--");
                ddlSelectForEdit.Items.Insert(0, "--Select a Role--");
            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
            }
        }
        protected void CreateUser(object o, EventArgs e)
        {
            try
            {
                UserManager manager = new UserManager();
                User user = new User();
                Code code = new Code();
                Employee employee = new Employee();
                code.CodeName = txtEmployeeCode.Value;
                employee.EmpCode = code;
                user.Employee = employee;
                user.UserName = txtUserName.Value;
                user.Password = txtPassword.Value;
                if(selectRole.Value=="--Select a Role--")
                {
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "Please Select a User Role";
                }
                else
                {
                    Role role = new Role();
                    role.Id = Convert.ToInt32(selectRole.Value);
                    user.Role = role;
                    msgLbl.Text = manager.CreateNewUser(user);
                    msgLbl.ForeColor = Color.Green;  
                }
              
            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
            }
        }

        protected void ResetAccount(object o, EventArgs e)
        {
            UserManager manager = new UserManager();
            Code code = new Code();
            code.CodeName = txtEmpCodeReset.Value;
            if (manager.ResetAccounts(code))
            {
                msgLbl.Text = "Account Reset Successfully";
                msgLbl.ForeColor = Color.Green;
            }
            else
            {
                msgLbl.Text = "Account Not Reset";
                msgLbl.ForeColor = Color.Red;
            }
        }
        protected void ChangeAccount(object sender, EventArgs e)
        {
            UserManager userManager = new UserManager();
            Code code = new Code();
            code.CodeName = Session["Code"].ToString();
            Employee employee = new Employee();
            employee.EmpCode = code;
            User user = new User();
            user.Employee = employee;
            user.UserName = txtUserNamechange.Value;
            user.Password = PsNew.Text;
            if (PsCurrent.Value == Session["Pass"].ToString())
            {
                if (userManager.ChangeUsernamePassword(user))
                {
                    msgLbl.Text = "Username and Password  Changed Successfully";
                    msgLbl.ForeColor = Color.Green;
                }
                else
                {
                    msgLbl.Text = "Username and Password Not Changed";
                    msgLbl.ForeColor = Color.Red;
                }

            }
        }
        protected void DeleteUser(object o, EventArgs e)
        {
            UserManager manager = new UserManager();
            Code code = new Code();
            code.CodeName = txtEmpCodeDelete.Value;
            if (manager.DeleteAccount(code))
            {
                msgLbl.Text = "Account Deleted Successfully";
                msgLbl.ForeColor = Color.Green;
            }
            else
            {
                msgLbl.Text = "Account Not Deleted";
                msgLbl.ForeColor = Color.Red;
            }
        }
        protected void FindUser(object o, EventArgs e)
        {
            UserManager manager = new UserManager();
            User user = new User();
            Code code = new Code();
            code.CodeName = txtEmpCodeEdit.Value;
            Employee employee = new Employee();
            employee.EmpCode = code;
            user.Employee = employee;
            user = manager.GetAccountsDetails(user);
            previousUserRole.ForeColor = Color.Green;
            previousUserRole.Font.Bold = true;

            switch (user.Role.Id)
            {
                case 1: previousUserRole.Text = "Managment"; break;
                case 2: previousUserRole.Text = "HR Manager"; break;
                case 3: previousUserRole.Text = "Admin"; break;
                case 4: previousUserRole.Text = "Project Manager"; break;
                case 5: previousUserRole.Text = "Employee"; break;
            }


        }
        protected void UpdateUser(object o, EventArgs e)
        {
            UserManager manager = new UserManager();
            User user = new User();
            Code code = new Code();
            code.CodeName = txtEmpCodeEdit.Value;
            Employee employee = new Employee();
            employee.EmpCode = code;
            user.Employee = employee;
            if (ddlSelectForEdit.Value == "--Select a Role--")
            {
                msgLbl.Text = "Please Select a User Role";
                msgLbl.ForeColor = Color.Red;
            }
            else
            {
                Role role = new Role();
                role.Id = Convert.ToInt32(ddlSelectForEdit.Value);
                user.Role = role;
                if (manager.EditAccount(user))
                {
                    msgLbl.Text = "Account Updated Successfully";
                    msgLbl.ForeColor = Color.Green;
                }
                else
                {
                    msgLbl.Text = "Account Not Updated";
                    msgLbl.ForeColor = Color.Red;
                }
            }
        }
        protected void SearchAccount(object o, EventArgs e)
        {
            User user=new User();
            UserManager manager=new UserManager();
            Code code=new Code();
            code.CodeName = txtEmpCodeSearch.Value;
            Employee employee=new Employee();
            employee.EmpCode = code;
            user.Employee = employee;
            user = manager.GetAccountsDetails(user);
            List<User>users=new List<User>();
            users.Add(user);
            serachAccountGdv.DataSource = users;
            serachAccountGdv.DataBind();
        }
    }


}